package com.example.blog_system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.example.blog_system.dao.CommentMapper;
import com.example.blog_system.dao.StatisticMapper;
import com.example.blog_system.model.domain.Comment;
import com.example.blog_system.model.domain.Statistic;
import com.example.blog_system.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private CommentMapper comnentMapper;
    @Autowired
    private StatisticMapper statisticMapper;
    /**
     *根据文章id分页查询评论
     */
    @Override
    public PageInfo<Comment> getComments(Integer aid, int page, int count) {
        PageHelper.startPage(page, count);
        List<Comment> commentList = comnentMapper.selectCommentWithPage(aid);
        PageInfo<Comment> commentInfo = new PageInfo(commentList);
        return commentInfo;
    }
    /**
        *用户发表评论
        @param comment
        */
    @Override
    public void pushComment(Comment comment) {
        comnentMapper.pushComment ( comment);
        //更新文章评论数据量
        Statistic statistic = statisticMapper.selectStatisticWithArticleId(comment. getArticleId());
        statistic.setCommentsNum(statistic. getCommentsNum()+1);
        statisticMapper.updateArticleCommentsWithId(statistic);
    }

}
